# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=qt-creator
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-devel")
_base_ver=16.0.0
pkgver=${_base_ver/-/}
pkgrel=1
pkgdesc='Lightweight, cross-platform integrated development environment (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io/product/development-tools'
msys2_repository_url="https://code.qt.io/cgit/qt-creator/qt-creator.git"
msys2_references=(
  'archlinux: qtcreator'
)
license=('spdx:GPL-3.0-or-later WITH Qt-GPL-exception-1.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-litehtml"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt6-translations"
             "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
             #"${MINGW_PACKAGE_PREFIX}-qt6-charts"
             "${MINGW_PACKAGE_PREFIX}-qt6-doc"
             "${MINGW_PACKAGE_PREFIX}-qt6-quicktimeline"
             "${MINGW_PACKAGE_PREFIX}-yaml-cpp")
install=qt-creator-${MSYSTEM}.install
_pkgfqn="${_realname}-opensource-src-${_base_ver}"
source=(https://download.qt.io/official_releases/qtcreator/${_base_ver%.*}/${_base_ver}/${_pkgfqn}.tar.xz
        qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch
        qt-creator-5.0.1-fix-library-archive-path.patch
        003-link-against-clang-cpp.patch
        005-rename-creator-libzip.patch)
sha256sums=('18b4353e68a31c062713008c05ce7f7ee88aaaf56d5ac0c6de579babfb2837da'
            'eacd98f69324695595690c5328d62137af609e55c7c94c19e7bd8e1f7e4b4712'
            '29d67f88f071abe7a4b589182767b8a697fe4e516d70707bfca88f035883718f'
            '5d6c0b626ef29ce39372eb180527d64e11af2bf39a1a3e84e709464a20d2433b'
            '417ef118818add39304ec36e4b3651d4fa238ed548cffeab42cbcf651a79294c')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_pkgfqn}

  apply_patch_with_msg \
    qt-creator-3.2.0-Allow-iOS-plugin-on-any-platform.patch \
    qt-creator-5.0.1-fix-library-archive-path.patch \
    003-link-against-clang-cpp.patch \
    005-rename-creator-libzip.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_DOCS=ON \
    -DBUILD_DEVELOPER_DOCS=ON \
    -DBUILD_QBS=OFF \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DWITH_TESTS=OFF \
    -DCLANGTOOLING_LINK_CLANG_DYLIB=ON \
    ../${_pkgfqn}

  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/cmake --build . --target docs
}

package_qt-creator() {
  pkgdesc="Lightweight, cross-platform integrated development environment (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-declarative"
           "${MINGW_PACKAGE_PREFIX}-qt6-tools"
           "${MINGW_PACKAGE_PREFIX}-qt6-svg"
           "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
           "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
           "${MINGW_PACKAGE_PREFIX}-qt6-translations"
           "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
           "${MINGW_PACKAGE_PREFIX}-litehtml"
           "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  optdepends=("${MINGW_PACKAGE_PREFIX}-gdb: for the debugger"
              "${MINGW_PACKAGE_PREFIX}-cmake: CMake project support"
              "${MINGW_PACKAGE_PREFIX}-ninja: Ninja Generator"
              "${MINGW_PACKAGE_PREFIX}-qbs: QBS project support"
              "${MINGW_PACKAGE_PREFIX}-clang-libs: ClangCodeModel and ClangFormat plugins"
              #"${MINGW_PACKAGE_PREFIX}-qt6-charts: Plugin AppStatisticsMonitor"
              "${MINGW_PACKAGE_PREFIX}-qt6-doc: integrated Qt6 documentation"
              "${MINGW_PACKAGE_PREFIX}-qt6-quicktimeline")

  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -d "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_pkgfqn}/LICENSE* -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt-creator-docs() {
  pkgdesc="Lightweight, cross-platform integrated development environment (documentation) (mingw-w64)"
  depends=()

  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install . --component qch_docs
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install . --component html_docs
}

package_qt-creator-devel() {
  pkgdesc="Lightweight, cross-platform integrated development environment (development files) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install . --component Devel
}

# Please leave this block here as I use
# it to quickly make and test changes.
# return 0
# Debugger ProjectExplorer 
# ALLGOOD="yes"
# PLUGINS="Debugger"
# for PLUGIN in $PLUGINS; do
#   PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#   pushd src/build-${MSYSTEM}/src/plugins/${PLUGINLC}
#     PATH=${MINGW_PREFIX}/bin:"$PATH" cmake --build . || ALLGOOD="no"
#   popd
# done
# if [ "${ALLGOOD}" = "yes" ]; then
# (
#   QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   while [ -n "$QTCPID" ]; do
#     cmd.exe /c "taskkill /PID $QTCPID"
#     sleep 1
#     QTCPID=$(ps --windows | grep creator | awk '{ print $1; }')
#   done
#   for PLUGIN in $PLUGINS; do
#     PLUGINLC=$(echo ${PLUGIN} | tr 'A-Z' 'a-z')
#     pushd src/build-${MSYSTEM}/src/plugins/${PLUGINLC}
#       PATH=${MINGW_PREFIX}/bin:$PATH cmake --build .
#       cp ../../../lib/qtcreator/plugins/${PLUGIN}d.dll ${MINGW_PREFIX}/lib/qtcreator/plugins/
#     popd
#   done
#   PATH=${MINGW_PREFIX}/bin:$PATH ${MINGW_PREFIX}/bin/qtcreator &
#   )
# fi
# 

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
